﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

//CID 0657 //2010.12.15 //Begin
namespace InfoJetSoft.Service.Worker
{
    class EXIAddItemsWorker
    {
        public static void addItems(Form form)
        {
            string contextId = form.ParamTable["xdoc_param_exi_context_id"] as string;
            string exiId = form.ParamTable["xdoc_param_exi_id"] as string;
            XmlElement contextElement = DOMUtil.findNodeByJetId(form, contextId) as XmlElement;
            if (contextElement != null)
            {
                contextElement = DOMUtil.selectOneNode(contextElement, "pc:BDCAssociatedEntity", form.XmlNsResolver) as XmlElement;

                string xml = form.ParamTable["xdoc_param_exi_added_xml"] as string;
                XmlDocument itemsDoc = new XmlDocument();
                itemsDoc.LoadXml(xml);

                EXIInfo exiInfo = (EXIInfo)form.Manifest.EXIInfoTable[exiId];
                if (exiInfo.MultiItem.Equals("0"))
                {
                    contextElement.InnerXml = "";
                }

                //CID 0750 //2010.12.30 //Begin
                string id1Filter = "pc:BDCEntity/pc:EntityId1";
                XmlNodeList id1ElementList = DOMUtil.selectNodes(contextElement, id1Filter, 0, form.XmlNsResolver);
                List<EXIIds> entityIdsList = new List<EXIIds>();
                foreach (XmlElement id1Element in id1ElementList)
                {
                    string id1 = id1Element.InnerText.Trim();
                    if (id1.Length <= 0)
                    {
                        contextElement.RemoveChild(id1Element.ParentNode);
                    }

                    XmlElement entityElement = id1Element.ParentNode as XmlElement;
                    entityIdsList.Add(new EXIIds(form, entityElement));
                }

                XmlNodeList itemElementList = itemsDoc.SelectNodes("/Items/Item");
                List<EXIIds> itemIdsList = new List<EXIIds>();
                foreach (XmlElement itemElement in itemElementList)
                {
                    itemIdsList.Add(new EXIIds(form, itemElement));
                }

                foreach (EXIIds itemIds in itemIdsList)
                {
                    bool found = false;
                    foreach (EXIIds entityIds in entityIdsList)
                    {
                        if (entityIds.IsSame(itemIds) )
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found)
                    {
                        string displayName = DOMUtil.getNodeValue(itemIds.Element.SelectSingleNode("DisplayName"));
                        string eir = DOMUtil.getNodeValue(itemIds.Element.SelectSingleNode("EIR"));

                        XmlElement entityElement = form.XmlDoc.CreateElement("pc", "BDCEntity", "http://schemas.microsoft.com/office/infopath/2007/PartnerControls");
                        contextElement.AppendChild(entityElement);
                        XmlElement displayNameElement = form.XmlDoc.CreateElement("pc", "EntityDisplayName", "http://schemas.microsoft.com/office/infopath/2007/PartnerControls");
                        displayNameElement.InnerText = displayName;
                        entityElement.AppendChild(displayNameElement);
                        XmlElement eirElement = form.XmlDoc.CreateElement("pc", "EntityInstanceReference", "http://schemas.microsoft.com/office/infopath/2007/PartnerControls");
                        eirElement.InnerText = eir;
                        entityElement.AppendChild(eirElement);
                        XmlElement id1Element = form.XmlDoc.CreateElement("pc", "EntityId1", "http://schemas.microsoft.com/office/infopath/2007/PartnerControls");
                        id1Element.InnerText = itemIds.Id1;
                        entityElement.AppendChild(id1Element);
                        XmlElement id2Element = form.XmlDoc.CreateElement("pc", "EntityId2", "http://schemas.microsoft.com/office/infopath/2007/PartnerControls");
                        id2Element.InnerText = itemIds.Id2;
                        entityElement.AppendChild(id2Element);
                        XmlElement id3Element = form.XmlDoc.CreateElement("pc", "EntityId3", "http://schemas.microsoft.com/office/infopath/2007/PartnerControls");
                        id3Element.InnerText = itemIds.Id3;
                        entityElement.AppendChild(id3Element);
                        XmlElement id4Element = form.XmlDoc.CreateElement("pc", "EntityId4", "http://schemas.microsoft.com/office/infopath/2007/PartnerControls");
                        id4Element.InnerText = itemIds.Id4;
                        entityElement.AppendChild(id4Element);
                        XmlElement id5Element = form.XmlDoc.CreateElement("pc", "EntityId5", "http://schemas.microsoft.com/office/infopath/2007/PartnerControls");
                        id5Element.InnerText = itemIds.Id5;
                        entityElement.AppendChild(id5Element);

                        //CID 0828 //2011.03.10 //Begin
                        if (form.FocusItemIndex == -1)
                        {
                            form.FocusItemIndex = DOMUtil.getChildElementIndex(contextElement, entityElement);
                        }
                        //CID 0828 //2011.03.10 //End
                    }
                }
                //CID 0750 //2010.12.30 //End
            }
        }
    }
}
//CID 0657 //2010.12.15 //End